package com.javatiaocao.myblog.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RedisServiceImpl {
    @Autowired
    private HashRedisServiceImpl hashRedisServiceImpl;
    /**
     * 访问量
     * */
    public long addVisitorNumToRedis(String visitor, Object field, long i) {
        boolean fieldIsExist = hashRedisServiceImpl.hasHashKey(visitor, field);
        if (fieldIsExist){
            return  hashRedisServiceImpl.hashIncrement(visitor,field,i);
        }
        return 0L;
    }

    /**
     *
     * 向redis保存访问量
     *
     * **/
    public long putVisitorNumToRedis(String visitor, Object field, long i) {
        hashRedisServiceImpl.put(visitor,field,i);
        return Long.valueOf(hashRedisServiceImpl.get(visitor,field).toString());
    }

    public long getVisitorNumOnRedis(String visitor, String pageName) {
        if(hashRedisServiceImpl.hasHashKey(visitor,pageName)){
            return Long.valueOf(hashRedisServiceImpl.get(visitor,pageName).toString());
        }
        return 0;
    }
}
